﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace graphical_function_explorer.FunctionPreparer
{
    class FormatController
    {
        private char funcVar;
        private string equation;

        public FormatController()
        {

        }

        public string ApplyConstraints(char mainVar, string equation)
        {
            funcVar = ItemProvider.GetFunctionVariable(mainVar);
            this.equation = equation;

            DeleteEmptySpaces();
            StandardizeBrackets();
            StandardizeOperationSigns();
            InsertMultiplicationSigns();

            return this.equation;
        }

        /* Removes the free spaces
         */
        private void DeleteEmptySpaces()
        {
            equation = equation.Replace(" ", "");
        }

        /* Replaces the follwing kind of brackets with normal ones:
         * - [] --> ()
         * - {} --> ()
         */
        private void StandardizeBrackets()
        {
            equation = equation.Replace("[", "(");
            equation = equation.Replace("]", ")");
            equation = equation.Replace("{", "(");
            equation = equation.Replace("}", ")");
        }

        /* Replaces the following possible representations for divisions:
         * - : --> /
         */
        private void StandardizeOperationSigns()
        {
            equation = equation.Replace(":", "/");
        }

        /* Inserts the multiplication operator * into the following substrings:
         * - Number( --> Number*(
         * - )Number --> )*Number
         * - Number Variable --> Number*Variable
         * - Variable Number --> Variable*Number
         * - Variable Variable --> Variable*Variable
         */
        private void InsertMultiplicationSigns()
        {
            equation = equation.Replace("0(", "0*(");
            equation = equation.Replace("1(", "1*(");
            equation = equation.Replace("2(", "2*(");
            equation = equation.Replace("3(", "3*(");
            equation = equation.Replace("4(", "4*(");
            equation = equation.Replace("5(", "5*(");
            equation = equation.Replace("6(", "6*(");
            equation = equation.Replace("7(", "7*(");
            equation = equation.Replace("8(", "8*(");
            equation = equation.Replace("9(", "9*(");
            equation = equation.Replace(funcVar + "(", funcVar + "*(");

            equation = equation.Replace(")0", ")*0");
            equation = equation.Replace(")1", ")*1");
            equation = equation.Replace(")2", ")*2");
            equation = equation.Replace(")3", ")*3");
            equation = equation.Replace(")4", ")*4");
            equation = equation.Replace(")5", ")*5");
            equation = equation.Replace(")6", ")*6");
            equation = equation.Replace(")7", ")*7");
            equation = equation.Replace(")8", ")*8");
            equation = equation.Replace(")9", ")*9");
            equation = equation.Replace(")" + funcVar, ")*" + funcVar);

            equation = equation.Replace("0" + funcVar, "0*" + funcVar);
            equation = equation.Replace("1" + funcVar, "1*" + funcVar);
            equation = equation.Replace("2" + funcVar, "2*" + funcVar);
            equation = equation.Replace("3" + funcVar, "3*" + funcVar);
            equation = equation.Replace("4" + funcVar, "4*" + funcVar);
            equation = equation.Replace("5" + funcVar, "5*" + funcVar);
            equation = equation.Replace("6" + funcVar, "6*" + funcVar);
            equation = equation.Replace("7" + funcVar, "7*" + funcVar);
            equation = equation.Replace("8" + funcVar, "8*" + funcVar);
            equation = equation.Replace("9" + funcVar, "9*" + funcVar);

            equation = equation.Replace(funcVar + "0", funcVar + "*0");
            equation = equation.Replace(funcVar + "1", funcVar + "*1");
            equation = equation.Replace(funcVar + "2", funcVar + "*2");
            equation = equation.Replace(funcVar + "3", funcVar + "*3");
            equation = equation.Replace(funcVar + "4", funcVar + "*4");
            equation = equation.Replace(funcVar + "5", funcVar + "*5");
            equation = equation.Replace(funcVar + "6", funcVar + "*6");
            equation = equation.Replace(funcVar + "7", funcVar + "*7");
            equation = equation.Replace(funcVar + "8", funcVar + "*8");
            equation = equation.Replace(funcVar + "9", funcVar + "*9");

            while (equation.Contains(funcVar.ToString() + funcVar))
                equation = equation.Replace(funcVar.ToString() + funcVar, funcVar + "*" + funcVar);
        }
    }
}
